package com.fr.report.core;

import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.base.core.antlr.ANTLRException;
import com.fr.data.core.Compare;
import com.fr.report.CellElement;
import com.fr.report.Report;
import com.fr.report.TableDataSource;
import com.fr.report.cellElement.Formula;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Calculator;
import com.fr.report.script.Primitive;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ScriptUtils
{
  public static final Object REC_CE_LIST = new Object();

  public static String[] getDependenceByCompare(Compare paramCompare)
  {
    Object localObject = paramCompare.getValue();
    if (localObject instanceof Formula)
      return getDependenceByFormula(((Formula)localObject).getContent());
    if (localObject instanceof Parameter)
      return new String[] { ((Parameter)localObject).getName() };
    return new String[0];
  }

  public static String[] getDependenceByParameter(Parameter[] paramArrayOfParameter)
  {
    ArrayList localArrayList = new ArrayList();
    for (int i = 0; i < paramArrayOfParameter.length; ++i)
      localArrayList.add(paramArrayOfParameter[i].toString());
    return ((String[])localArrayList.toArray(new String[localArrayList.size()]));
  }

  public static String[] getDependenceByFormula(String paramString)
  {
    ArrayList localArrayList = new ArrayList();
    try
    {
      ColumnRow[] arrayOfColumnRow = Calculator.relatedColumnRowArray(paramString);
      for (int i = 0; i < arrayOfColumnRow.length; ++i)
        localArrayList.add(arrayOfColumnRow[i].toString());
      String[] arrayOfString = Calculator.relatedParameters(paramString);
      localArrayList.addAll(Arrays.asList(arrayOfString));
    }
    catch (ANTLRException localANTLRException)
    {
      FRContext.getLogger().log(Level.WARNING, localANTLRException.getMessage(), localANTLRException);
    }
    return ((String[])localArrayList.toArray(new String[localArrayList.size()]));
  }

  public static String moveRow(String paramString, int paramInt1, int paramInt2)
  {
    return changePosition(paramString, paramInt1, paramInt2, -1, 0);
  }

  public static String moveColumn(String paramString, int paramInt1, int paramInt2)
  {
    return changePosition(paramString, -1, 0, paramInt1, paramInt2);
  }

  public static String changePosition(String paramString, int paramInt1, int paramInt2, int paramInt3, int paramInt4)
  {
    if (paramString.startsWith("="))
      paramString = paramString.substring(1);
    return Calculator.createCalculator().onAddDeleteColumnOrRow(paramString, paramInt1, paramInt2, paramInt3, paramInt4);
  }

  public static void executeCellElementFormula(Calculator paramCalculator, Formula paramFormula, Report paramReport, TableDataSource paramTableDataSource, CellElement paramCellElement)
  {
    LinkedList localLinkedList;
    try
    {
      localLinkedList = (LinkedList)paramCalculator.getAttribute(REC_CE_LIST);
      if (localLinkedList.contains(paramCellElement))
        throw new DeathCycleException("Death cycle exists when calculate : " + paramCellElement + "\n" + localLinkedList);
      localLinkedList.add(paramCellElement);
      paramFormula.setResult(executeFormula(paramCalculator, paramFormula, paramReport, paramTableDataSource, ColumnRow.valueOf(paramCellElement.getColumn(), paramCellElement.getRow())));
      localLinkedList.removeLast();
    }
    catch (Throwable localThrowable)
    {
      throw new RuntimeException("Error ColumnRow:" + ColumnRow.valueOf(paramCellElement.getColumn(), paramCellElement.getRow()) + "\tcontent:" + paramFormula.getContent() + localThrowable.getMessage(), localThrowable);
    }
  }

  public static Object executeFormula(Calculator paramCalculator, Formula paramFormula)
  {
    return executeFormula(paramCalculator, paramFormula, paramCalculator.getCurrentColumnRow());
  }

  public static Object executeFormula(Calculator paramCalculator, Formula paramFormula, ColumnRow paramColumnRow)
  {
    return executeFormula(paramCalculator, paramFormula, paramCalculator.getCurrentReport(), paramCalculator.getCurrentTableDataSource(), paramColumnRow);
  }

  public static Object executeFormula(Calculator paramCalculator, Formula paramFormula, Report paramReport, TableDataSource paramTableDataSource, ColumnRow paramColumnRow)
  {
    String str;
    try
    {
      str = paramFormula.getContent().substring(1);
      paramCalculator.setCurrentReport(paramReport);
      paramCalculator.setCurrentTableDataSource(paramTableDataSource);
      paramCalculator.setCurrentColumnRow(paramColumnRow);
      Object localObject = paramCalculator.eval(str);
      if (localObject instanceof Number)
      {
        if (localObject instanceof Double)
        {
          double d = ((Double)localObject).doubleValue();
          if ((!(Double.isNaN(d))) && (!(Double.isInfinite(d))))
            break label103;
          return "∞";
        }
        if ((localObject instanceof Integer) && (2147483647 == ((Integer)localObject).intValue()))
          return "∞";
      }
      label103: return localObject;
    }
    catch (Throwable localThrowable)
    {
      if (paramColumnRow != null)
        System.err.println("Error ColumnRow:" + paramColumnRow + "\tcontent:" + paramFormula);
      else
        System.err.println("Error content:" + paramFormula);
      FRContext.getLogger().log(Level.WARNING, localThrowable.getMessage(), localThrowable);
    }
    return Primitive.ERROR_NAME;
  }
}